package com.own.business.resource.controller;

import com.own.business.resource.business.form.SignatureCommonForm;
import com.own.business.resource.business.service.ResourceService;
import com.own.business.resource.controller.form.SignatureForm;
import com.own.business.resource.core.method.SignatureBusinessMethod;
import com.own.business.resource.core.model.SignatureModel;
import com.own.business.resource.entity.vo.FileRecordVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ResourceController
 *
 * @author chenxueli
 * @date 2023-07-07 14:49:41
 */
@Tag(name = "资源文件" + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "resource")
public class ResourceController extends BaseController {

    @Resource
    private ResourceService resourceService;

    @Resource
    private SignatureBusinessMethod signatureBusinessMethod;

    @LogOperationAop
    @Operation(summary = "上传文件")
    @PostMapping(value = "/upload")
    public ResultModel<FileRecordVo> upload(
            @Parameter(description = "上传方式 默认default") @RequestParam(defaultValue = "default", required = false) String method,
            @Parameter(description = "上传的文件", required = true) @RequestPart MultipartFile file
    ) {
        var vo = resourceService.upload(method, file);
        return R.success(vo);
    }

    @LogOperationAop
    @Operation(summary = "上传文件（批量）")
    @PostMapping(value = "/batch/upload")
    public ResultModel<List<FileRecordVo>> upload(
            @Parameter(description = "上传方式 默认default") @RequestParam(defaultValue = "default", required = false) String method,
            @Parameter(description = "上传的文件（多个）", required = true)
            @ArraySchema(schema = @Schema(type = "string", format = "binary"))
            @RequestPart("files")
            MultipartFile[] files
    ) {
        var voList = Arrays.stream(files)
                .map(file -> resourceService.upload(method, file))
                .collect(Collectors.toList());
        return R.success(voList);
    }

    @LogOperationAop
    @Operation(summary = "获取上传签名信息")
    @GetMapping(value = "/signature")
    public ResultModel<SignatureModel> signature(
            @Parameter(description = "上传方式 默认default") @RequestParam(defaultValue = "default", required = false) String method,
            @Parameter(description = "文件名称", required = true) @RequestParam String fileName
    ) {
        var signature = resourceService.signature(method, fileName);
        return R.success(signature);
    }

    @LogOperationAop
    @Operation(summary = "获取上传签名信息（common）")
    @PostMapping(value = "/common/signature")
    public ResultModel<SignatureModel> signature(
            @Parameter(description = "上传方式 默认default") @RequestParam(defaultValue = "default", required = false) String method,
            @Parameter(description = "文件名称", required = true) @RequestParam String fileName,
            @RequestBody SignatureCommonForm form
    ) {
        form.check();
        var signature = resourceService.signature(method, fileName, form);
        return R.success(signature);
    }

    @LogOperationAop
    @Operation(summary = "获取上传签名信息（业务判断）")
    @PostMapping(value = "/signature")
    public ResultModel<SignatureModel> signatureForBusiness(
            @Parameter(description = "文件名称", required = true) @RequestParam String fileName,
            @RequestBody SignatureForm form
    ) {
        form.check();
        // 根据业务去获取method
        var method = signatureBusinessMethod.getMethodName(form.getModule(), form.getSign());
        var signature = resourceService.signature(method, fileName, form);
        return R.success(signature);
    }

}
